package br.com.simplex.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Util {

	public static String lerArquivo(String nomeArquivo) throws IOException {
		StringBuffer stringBuffer = new StringBuffer();
		BufferedReader reader = new BufferedReader(new FileReader(nomeArquivo));
		char [] buffer = new char[1024];
		int numRead;
		while((numRead = reader.read(buffer)) != -1){
			stringBuffer.append(String.valueOf(buffer, 0,numRead));
			buffer = new char[1024];
		}
		reader.close();
		return stringBuffer.toString();
	}
	
	/**
	 * Salva um arquivo com o conteudo informado
	 * @param nomeArquivo
	 * @param conteudo
	 * @throws IOException
	 */
	public static void salvarArquivo(String nomeArquivo, String conteudo) throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter(nomeArquivo));
		writer.write(conteudo);
		writer.flush();
		writer.close();
	}
	
	public static int getIndexOfLastNumber(String string){
		char[] string_char = string.toCharArray();
		for(int i = 0;i < string.length();i++){
			if(!Character.isDigit(string_char[i])){
				return i;
			}
		}
		return -1;
	}

}
